package com.zmn.uac.business.impl.user;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.consts.GlobalConsts;
import com.zmn.uac.business.interfaces.user.UserCenterBService;
import com.zmn.uac.business.interfaces.wechat.WeiChatBService;
import com.zmn.uac.client.UserRegisterClient;
import com.zmn.uac.model.bo.user.UserThirdInfoRegisterBO;
import com.zmn.uac.model.vo.TagNamesVO;
import com.zmn.uuc.common.dio.user.UserRegisterByThirdAccountDIO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：用户中心业务实现
 *
 * @author xujie
 * @since 2021/05/19 14:41
 */
@Slf4j
@Service
public class UserCenterBServiceImpl implements UserCenterBService {

    private static final String TAG = "用户中心业务实现";

    @Resource
    WeiChatBService weiChatBusinessService;

    @Resource
    UserRegisterClient userRegisterClient;

    @Override
    public ResponseDTO<Long> registerUserThird(UserThirdInfoRegisterBO userThirdInfoRegisterBO) {
        log.info("[{}], 三方信息注册入参userThirdInfoRegisterBO:{}", TAG, JSONObject.toJSONString(userThirdInfoRegisterBO));
        UserRegisterByThirdAccountDIO registerByThirdAccount = setUserRegisterByThirdAccountDIO(userThirdInfoRegisterBO);
        Long userId = userRegisterClient.registerByThirdAccount(registerByThirdAccount);
        log.info("[{}], 三方信息注册出参:{}", TAG, userId);

        return new ResponseDTO<>(userId);
    }

    private String getTagNames(String appId, String appSecret, List<Integer> tagIds) {
        String tagNames = GlobalConsts.SPLITER_EMPTY;
        StringBuilder stringBuilder = new StringBuilder();
        List<TagNamesVO> tagNamesVOList = weiChatBusinessService.getTagNames(appId, appSecret);
        if (CollectionUtils.isNotEmpty(tagNamesVOList)) {
            Map<Integer, String> map = tagNamesVOList.stream().collect(Collectors.toMap(TagNamesVO::getId,
                    TagNamesVO::getName));
            for (Integer tagId : tagIds) {
                if (Objects.nonNull(tagId)) {
                    stringBuilder.append(map.get(tagId)).append(GlobalConsts.SPLITER_DEFAULT);
                }
            }
            if (stringBuilder.length() > 0) {
                tagNames = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
            }
        }
        return tagNames;
    }

    private UserRegisterByThirdAccountDIO setUserRegisterByThirdAccountDIO(UserThirdInfoRegisterBO userThirdInfoRegisterBO){
        UserRegisterByThirdAccountDIO registerByThirdAccount = new UserRegisterByThirdAccountDIO();
        registerByThirdAccount.setMobile(userThirdInfoRegisterBO.getMobile());
        registerByThirdAccount.setShopId(userThirdInfoRegisterBO.getShopId());
        registerByThirdAccount.setOpenId(userThirdInfoRegisterBO.getOpenId());
        registerByThirdAccount.setUnionId(StringUtils.isNotBlank(userThirdInfoRegisterBO.getUnionId()) ? userThirdInfoRegisterBO.getUnionId() : userThirdInfoRegisterBO.getOpenId());
        registerByThirdAccount.setAvatarSrc(userThirdInfoRegisterBO.getAvatarSrc());
        registerByThirdAccount.setNickName(userThirdInfoRegisterBO.getNickName());
        registerByThirdAccount.setNation(userThirdInfoRegisterBO.getCountry());
        registerByThirdAccount.setProvince(userThirdInfoRegisterBO.getProvince());
        registerByThirdAccount.setCity(userThirdInfoRegisterBO.getCity());
        registerByThirdAccount.setSourceParam(userThirdInfoRegisterBO.getSourceParam());
        registerByThirdAccount.setPlat(userThirdInfoRegisterBO.getPlat());
        registerByThirdAccount.setGender(userThirdInfoRegisterBO.getGender());
        registerByThirdAccount.setChannelId(userThirdInfoRegisterBO.getChannelId());
        registerByThirdAccount.setChannelName(userThirdInfoRegisterBO.getChannelName());
        registerByThirdAccount.setEntrance(userThirdInfoRegisterBO.getEntrance());
        registerByThirdAccount.setAppId(userThirdInfoRegisterBO.getAppId());
        registerByThirdAccount.setAppName(userThirdInfoRegisterBO.getAppName());
        registerByThirdAccount.setLanguage(userThirdInfoRegisterBO.getLanguage());
        registerByThirdAccount.setSubscribeTime(userThirdInfoRegisterBO.getSubscribeTime());
        registerByThirdAccount.setRemark(userThirdInfoRegisterBO.getRemark());
        registerByThirdAccount.setGroupId(userThirdInfoRegisterBO.getGroupId());
        String tagNames = userThirdInfoRegisterBO.getTagNames();
        if (CollectionUtils.isNotEmpty(userThirdInfoRegisterBO.getTagIds())) {
            registerByThirdAccount.setTagIds(userThirdInfoRegisterBO.getTagIds().stream().map(String::valueOf).collect(Collectors.joining(GlobalConsts
                    .SPLITER_DEFAULT)));
            // 获取标签名称
            tagNames = getTagNames(userThirdInfoRegisterBO.getAppId(), userThirdInfoRegisterBO.getAppSecret(), userThirdInfoRegisterBO.getTagIds());
        }
        registerByThirdAccount.setTagNames(tagNames);
        registerByThirdAccount.setSubscribeScene(userThirdInfoRegisterBO.getSubscribeScene());
        registerByThirdAccount.setQrScene(userThirdInfoRegisterBO.getQrScene());
        registerByThirdAccount.setQrSceneStr(userThirdInfoRegisterBO.getQrSceneStr());
        Integer subscribe = Objects.equals(GlobalConsts.NO, userThirdInfoRegisterBO.getSubscribe()) ? GlobalConsts.YES : GlobalConsts.NO;
        registerByThirdAccount.setSubscribe(subscribe);

        return registerByThirdAccount;
    }
}
